r,y,g=map(int,input().split())

sum=g+y+r

def ts(light,time,result):
    global g,y,r
    t=0
    if light==1:
        t=(g+y+r-time+result)%sum
    elif light==2:
        t=(g+y-time+result)%sum
    elif light==3:
        t=(g-time+result)%sum
    if t<=g:
        return 0
    elif t<=g+y:
        return y-(t-g)+r
    else:
        return r-(t-g-y)

result=0

n=int(input())
for i in range(0,n):
    light,time=map(int,input().split())
    if light==0:
        result+=time
    else:
        result+=ts(light,time,result)

print(result)
